package com.bsb.biz.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bsb.base.common.config.Const;
import com.bsb.base.mapper.SysOrganizationMapper;
import com.bsb.base.model.SysOrganization;
import com.bsb.base.model.SysOrganizationExample;
import com.bsb.base.model.SysOrganizationExample.Criteria;
import com.bsb.biz.shiro.UserUtils;
import com.bsb.base.model.SysUser;




@Service
public class SysOrganizationService{
	
	@Resource
	private SysOrganizationMapper sysOrganizationMapper;
	@Resource
	private SysUserService sysUserService;
	
	public void addSysOrganization(SysOrganization sysOrganization) {
		sysOrganizationMapper.insertSelective(sysOrganization);
	}
	
	public void delSysOrganizationById(Long id) {
		sysOrganizationMapper.deleteByPrimaryKey(id);
	}
	
	public void updateSysOrganization(SysOrganization sysOrganization) {
		sysOrganizationMapper.updateByPrimaryKeySelective(sysOrganization);
	}
	
	public void updateSysOrganizationByExample(SysOrganization sysOrganization,SysOrganizationExample sysOrganizationExample){
	    sysOrganizationMapper.updateByExampleSelective(sysOrganization,sysOrganizationExample);
	}
	
	public SysOrganization getSysOrganizationById(Long id) {
		return sysOrganizationMapper.selectByPrimaryKey(id);
	}
	
	public List<SysOrganization> getPageSysOrganization(SysOrganizationExample sysOrganizationExample) {
		return sysOrganizationMapper.selectPage(sysOrganizationExample);
	}
	
	public int countSysOrganization(SysOrganizationExample sysOrganizationExample) {
		return sysOrganizationMapper.countByExample(sysOrganizationExample);
	}
	
	public List<SysOrganization> selectByExample(SysOrganizationExample example){
	    return sysOrganizationMapper.selectByExample(example);
	}

	public List<SysOrganization> findAllWithExclude(SysOrganization source) {
		SysOrganizationExample example = new SysOrganizationExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdNotEqualTo(source.getId());
		criteria.andParent_idsNotLike(source.makeSelfAsParentIds()+"%");
		List<SysOrganization> list = sysOrganizationMapper.selectByExample(example );
		return list;
	}
	
	public void move(SysOrganization source, SysOrganization target) {
		source.setParent_id(target.getId());
		source.setParent_ids(target.getParent_ids());
		sysOrganizationMapper.updateByPrimaryKeySelective(source);
		String makeSelfAsParentIds = source.makeSelfAsParentIds();
		SysOrganizationExample example = new SysOrganizationExample();
		Criteria criteria = example.createCriteria();
		criteria.andParent_idsLike(makeSelfAsParentIds+"%");
		List<SysOrganization> list = sysOrganizationMapper.selectByExample(example );
		String makeSelfAsParentIds2 = target.makeSelfAsParentIds();
		for (SysOrganization sysOrganization : list) {
			sysOrganization.setParent_ids(makeSelfAsParentIds2+sysOrganization.getParent_ids().substring(makeSelfAsParentIds.length()));
			sysOrganizationMapper.updateByPrimaryKeySelective(sysOrganization);
		}
	}
	
	/**
	 * 子部门列表
	 * @param parentId
	 * @return
	 */
	public List<Long> queryDetpIdList(Long parentId) {
		return sysOrganizationMapper.queryDetpIdList(parentId);
	}
	
	public List<Long> getSubDeptIdList(Long organization_id) {
		//部门及子部门ID列表
		List<Long> deptIdList = new ArrayList<>();

		//获取子部门ID
		List<Long> subIdList = queryDetpIdList(organization_id);
		getDeptTreeList(subIdList, deptIdList);

		return deptIdList;
	}
	/**
	 * 某用户相关部门 (本部门以及子部门)
	 * @param userId
	 * @return
	 */
	public List<SysOrganization> getSubDeptList(){
		SysUser user = UserUtils.currentUser();
		Long userId = user.getId();
		List<SysOrganization> menuList = new ArrayList<SysOrganization>();
		if(Const.SUPER_ADMIN == userId){
			menuList = sysOrganizationMapper.selectByExample(null);
		}else{
			SysOrganizationExample example = new SysOrganizationExample();
			Criteria criteria = example.createCriteria();
			criteria.andAvailableEqualTo(true);
			criteria.andIdIn(sysUserService.getDeptIdsByUser());
			menuList = sysOrganizationMapper.selectByExample(example );
		}
		for (SysOrganization sysOrganization : menuList) {
			SysOrganization parentMenu = sysOrganizationMapper.selectByPrimaryKey(sysOrganization.getParent_id());
			if(null != parentMenu){
				sysOrganization.setParentName(parentMenu.getName());
			}
		}
		return menuList;
	}
	
	/**
	 * 递归
	 */
	private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList){
		for(Long deptId : subIdList){
			List<Long> list = queryDetpIdList(deptId);
			if(list.size() > 0){
				getDeptTreeList(list, deptIdList);
			}

			deptIdList.add(deptId);
		}
	}
	
	

}
